<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <!-- el-row 和 el-col一般是配合使用 这两个标签在任何el标签中都可以使用的 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="home" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hAdd('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <!-- 组织架构树形展示 -->
        <el-tree
          default-expand-all
          :data="list"
        >
          <template #default="{data}">
            <el-row type="flex" justify="space-between" align="middle" style="height: 40px;width:100%">
              <el-col :span="20">
                <svg-icon icon-class="home" /><span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span>
                        操作<i class="el-icon-arrow-down" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hAdd(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hEdit(data.id)">修改部门</el-dropdown-item>
                        <el-dropdown-item v-if="data.children.length === 0" @click.native="hDel(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
      <!-- 新增部门DiaLog -->
      <el-dialog
        :title="isEdit?'修改部门':'新增子部门'"
        :visible.sync="showDialog"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        @close="resetForm"
      >
        <!-- 父传子 -->
        <!-- // 解决公用组件数据不更新的方式1 v-if -->
        <!-- <addOrEdit v-if="showDialog" :id="curId" :is-edit="isEdit" @success="hSuccess" /> -->
        <addOrEdit :id="curId" ref="refAdd" :is-edit="isEdit" :origin-list="originList" @close="showDialog=false" @success="hSuccess" />
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { delDepartment, getDepartment } from '@/api/department'
import { tranDateToTreeDate } from '@/utils/index'
import addOrEdit from './deptDialog.vue'
export default {
  components: {
    addOrEdit
  },
  data() {
    return {
      isEdit: false, // false为新增 || true为修改
      // 控制新增的dialog是否显示
      showDialog: false,
      curId: '',
      list: [
        {
          name: '财务部',
          manager: '刘备',
          children: [
            {
              name: '财务核算部',
              manager: '张飞',
              children: []
            },
            {
              name: '税务核算部',
              manager: '关羽',
              children: []
            }
          ]
        }],
      originList: []
      // defaultProps: {
      //   children: 'children',
      //   label: 'name'
      // }
    }
  },
  created() {
    this.loadDepartment()
  },
  methods: {
    resetForm() {
      this.$refs.refAdd.reSet()
    },
    hSuccess() {
      this.loadDepartment()
      this.showDialog = false
    },
    // 获取树形数据
    async loadDepartment() {
      try {
        const res = await getDepartment()
        console.log('loadDepartment', res)
        this.originList = res.data.depts.map(item => {
          return { id: item.id, pid: item.pid, code: item.code, name: item.name }
        })
        // console.log('originList', this.originList)
        res.data.depts.shift()
        this.list = tranDateToTreeDate(res.data.depts)
      } catch (error) {
        console.log(error)
      }
    },
    // 新增部门
    hAdd(id) {
      this.curId = id
      this.showDialog = true
      this.isEdit = false
    },
    // 修改部门
    hEdit(id) {
      this.curId = id
      this.showDialog = true
      // 解决公用组件数据不更新的方式2 nextTick
      // 想拿子组件的数据就可以用 ref来拿到子组件的对象
      this.$nextTick(() => {
        this.$refs.refAdd.loadDepartmentById()
      })
      this.isEdit = true
    },
    // 删除部门
    hDel(id) {
      this.$confirm('你确定要删除这条数据吗?').then(async() => {
        const res = await delDepartment(id)
        if (res.code !== 10000) return this.$message.error(res.message)
        this.$message.success(res.message)
        // 重新渲染
        this.loadDepartment()
        console.log(res)
      }).catch(err => console.log(err))
    }
  }
}
</script>

